package com.lx.video.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lx.bilibili.entity.Danmu;
import com.lx.video.dao.DanmuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2025-04-06
 */
@RestController
@RequestMapping("/danmu")
public class DanmuController {

    @Autowired
    DanmuMapper danmuMapper;

    @RequestMapping("savedanmu")
    public int savedanmu(@RequestBody Danmu danmu) {
        if(danmu.getUid() == null || "".equals(danmu.getUid())){
            return 0;
        }
        danmu.setTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        return danmuMapper.insert(danmu);
    }

    @RequestMapping("getDanmuByVid")
    public List<Danmu> getDanmuByVid(int id) {
        LambdaQueryWrapper<Danmu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Danmu::getVid, id);
        return danmuMapper.selectList(lqw);
    }
}
